<template>
  <div class="bar-chart"  />
</template>

<script>
import * as echarts from 'echarts'
require('echarts/theme/macarons') // echarts theme
import resize from './mixins/resize'
// import { Handler } from 'mockjs'

// const animationDuration = 6000

export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '182px'
    },
    chartData: {
      type: Object,
      required: true
    }
  },
  data() {
    return {
      chart: null
    }
  },
  watch: {
    chartData: {
      deep: true,
      immediate: true,
      handler(newVal, oldVal) {
        // console.log(newVal, oldVal);
        if (!oldVal) return
        this.$nextTick(() => {
          this.chart.setOption(newVal)
        })
      }
    }
  },
  mounted() {
    this.$nextTick(() => {
      this.initChart()
    })
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    initChart() {
      this.chart = echarts.init(this.$el, 'macarons')

      this.chart.setOption(this.chartData, true)
      // debugger
    }
  }
}
</script>
<style>
.bar-chart{
  height: 280px; 
  width: 340px;
}
</style>
